package me.zk.practice1.hw;

import java.util.*;

public class HWBJ21 {
    //13
    //3,3,7,4,4,4,4,7,7,3,5,5,5
    //58,80,68,24,39,76,66,16,100,55,53,80,55
    public  static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        Map<Integer, S> map = new HashMap<>();

        int len = Integer.parseInt(scanner.nextLine());

        String s1 = scanner.nextLine();

        String s2 = scanner.nextLine();

        String[] strings1 = s1.split(",");

        String[] strings2 = s2.split(",");

        for (int i = 0; i < len; i++) {
            S s = null;
            Integer key = Integer.parseInt(strings1[i]);
            if(map.containsKey(key)) {
                s = map.get(key);
            } else {
                s = new S();
                s.id = key;
            }
            s.list.add(Integer.parseInt(strings2[i]));
            map.put(key,s);
        }
        //
        List<S> list= new ArrayList<>();
        for (Integer k :map.keySet()) {
            if (map.get(k).list.size() >= 3){
                Collections.sort(map.get(k).list);
                list.add(map.get(k));
            }
        }
        list.sort((o1, o2) -> {
            List<Integer> l1 = o1.list;
            int s11 = l1.get(0) + l1.get(1) + l1.get(2);

            List<Integer> l2 = o2.list;
            int s21 = l2.get(0) + l2.get(1) + l2.get(2);
            if (s11 > s21) {
                return -1;
            } else if (s11 < s21) {
                return 1;
            } else {
                if (o1.id > o2.id) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i).id);
            if (i != list.size()-1) {
                sb.append(",");
            }
        }
        System.out.println(sb.toString());



    }

  static   class S {
        public Integer id;

        public List<Integer> list = new ArrayList<>();

    }
}
